package com.ruoyi.web.controller.agoniapp;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.JwtUtils;
import com.ruoyi.common.utils.TokenUser;
import com.ruoyi.common.utils.UserIdByRequest;
import com.ruoyi.system.pojo.entity.VirtualStore;
import com.ruoyi.system.pojo.vo.HasStoreVO;
import com.ruoyi.system.service.IAgoniVirtualStoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/agoniapp/virtualStore")
public class VirtualStoreController {
    @Autowired
    private IAgoniVirtualStoreService agoniVirtualStoreService;

    @GetMapping("/listStores")
    public AjaxResult listStores() {
        return AjaxResult.success(agoniVirtualStoreService.listStores());
    }

    @PostMapping("/addStore")
    public AjaxResult addStore(@RequestBody VirtualStore virtualStore) {
        return AjaxResult.success(agoniVirtualStoreService.addStore(virtualStore));
    }

    /**
     * 根据设施ID获取商店列表
     * @param facilityId
     * @return
     */
    @GetMapping("/getStoresByFacilityId/{facilityId}")
    public AjaxResult getStoresByFacilityId(@PathVariable Long facilityId) {
        return AjaxResult.success(agoniVirtualStoreService.getStoresByFacilityId(facilityId));
    }

    /**
     * 根据token中的userId获取判断当前用户是否有商店列表
     * n
     */
    @GetMapping("/hasStoreByUserId")
    public AjaxResult hasStoreByUserId(HttpServletRequest request) {


        Long userId = UserIdByRequest.getUserId(request);
        HasStoreVO hasStore = agoniVirtualStoreService.hasStoreByUserId(userId);
        if (hasStore.isHasStore()){
            return AjaxResult.success("存在",hasStore);
        }

        return AjaxResult.success("不存在",hasStore);
    }

    //创建商店
    @PostMapping("/createStore")
    public AjaxResult createStore(@RequestBody VirtualStore virtualStore,HttpServletRequest request) {
        Long  userId= UserIdByRequest.getUserId(request);
        log.info("createStore: " + virtualStore);
        Boolean result= agoniVirtualStoreService.createStore(virtualStore,userId);
        if (result)
        {
            return AjaxResult.success("创建成功",result);
        }
        return AjaxResult.error("创建失败");
    }

}
